package com.zlc.platform.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zlc.common.common.constant.NotificationConstant;
import com.zlc.common.core.sys.mapper.MayanSysNotificationMapper;
import com.zlc.common.core.sys.model.vo.MayanSysNotificationVo;
import com.zlc.common.core.sys.service.MayanSysNotificationService;
import com.zlc.common.util.JsonHelper;
import com.zlc.platform.service.NotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class NotificationServiceImpl implements NotificationService {

    @Autowired
    private MayanSysNotificationService mayanSysNotificationService;

    @Override
    public void sendNotification(String toUserId, String title, String content) {
        MayanSysNotificationVo notificationVo = initNotification(NotificationConstant.NOTIFICATION_TYPE_SYSTEM, null, null, toUserId, title, content, null, null);
        mayanSysNotificationService.saveVo(notificationVo);
    }

    @Override
    public void sendNotification(String toUserId, String title, String content, String linkUri) {
        MayanSysNotificationVo notificationVo = initNotification(NotificationConstant.NOTIFICATION_TYPE_SYSTEM, null, null, toUserId, title, content, linkUri, null);
        mayanSysNotificationService.saveVo(notificationVo);
    }

    @Override
    public void sendNotification(String notificationCode, String toUserId, String title, String content, String linkUri) {
        MayanSysNotificationVo notificationVo = initNotification(NotificationConstant.NOTIFICATION_TYPE_SYSTEM, notificationCode, null, toUserId, title, content, linkUri, null);
        mayanSysNotificationService.saveVo(notificationVo);
    }

    @Override
    public void sendNotification(List<String> toUserIds, String title, String content) {
        sendNotification(NotificationConstant.NOTIFICATION_TYPE_SYSTEM, toUserIds, title, content, null);
    }

    @Override
    public void sendNotification(String notificationCode, List<String> toUserIds, String title, String content) {
        sendNotification(notificationCode, toUserIds, title, content, null);
    }

    @Override
    public void sendNotification(String notificationCode, List<String> toUserIds, String title, String content, String linkUri) {
        List<MayanSysNotificationVo> notificationVos = new ArrayList<>();
        for (String toUserId : toUserIds) {
            MayanSysNotificationVo notificationVo = initNotification(NotificationConstant.NOTIFICATION_TYPE_SYSTEM, notificationCode, null, toUserId, title, content, linkUri, null);
            notificationVos.add(notificationVo);
        }
        mayanSysNotificationService.saveBatchVoList(notificationVos);
    }

    @Override
    public void sendNotification(List<String> toUserIds, String title, String content, String linkUri) {
        sendNotification(NotificationConstant.NOTIFICATION_TYPE_SYSTEM, toUserIds, title, content, linkUri);
    }

    @Override
    public void sendPublicNotification(String title, String content) {
        MayanSysNotificationVo notificationVo = initNotification(NotificationConstant.NOTIFICATION_TYPE_PUBLIC, null, null, null, title, content, null, null);
        mayanSysNotificationService.saveVo(notificationVo);
    }

    @Override
    public void sendPublicNotification(String title, String content, String linkUri) {
        MayanSysNotificationVo notificationVo = initNotification(NotificationConstant.NOTIFICATION_TYPE_PUBLIC, null, null, null, title, content, linkUri, null);
        mayanSysNotificationService.saveVo(notificationVo);
    }

    @Override
    public void sendNotification(MayanSysNotificationVo notificationVo) {
        log.info("开始发送通知消息体:{}", JsonHelper.toJson(notificationVo));
        initNotification(notificationVo);
        mayanSysNotificationService.saveVo(notificationVo);
    }

    private void initNotification(MayanSysNotificationVo notificationVo) {
        notificationVo.setSendTime(new Date());
        notificationVo.setReadFlag(Boolean.FALSE);
        if (ObjectUtils.isEmpty(notificationVo.getNotificationType())) {
            //用户消息
            if (!ObjectUtils.isEmpty(notificationVo.getFromUserId())) {
                notificationVo.setNotificationType(NotificationConstant.NOTIFICATION_TYPE_USER);
            } else if (!ObjectUtils.isEmpty(notificationVo.getToUserId())) {
                notificationVo.setNotificationType(NotificationConstant.NOTIFICATION_TYPE_SYSTEM);
            } else {
                notificationVo.setNotificationType(NotificationConstant.NOTIFICATION_TYPE_PUBLIC);
            }
        }
    }

    private MayanSysNotificationVo initNotification(String notificationType, String notificationCode,
                                                    String fromUserId, String toUserId, String title, String content, String linkUri, String extendInfo) {
        MayanSysNotificationVo notificationVo = new MayanSysNotificationVo();
        notificationVo.setNotificationType(notificationType);
        notificationVo.setNotificationCode(notificationCode);
        notificationVo.setFromUserId(fromUserId);
        notificationVo.setToUserId(toUserId);
        notificationVo.setTitle(title);
        notificationVo.setContent(content);
        notificationVo.setLinkUri(linkUri);
        notificationVo.setExtendInfo(extendInfo);
        notificationVo.setReadFlag(false);
        notificationVo.setShowFlag(false);
        initNotification(notificationVo);
        return notificationVo;
    }

    @Override
    public void sendNotifications(List<MayanSysNotificationVo> notificationVos) {
        for (MayanSysNotificationVo notificationVo : notificationVos) {
            initNotification(notificationVo);
        }
        mayanSysNotificationService.saveBatchVoList(notificationVos);
    }

}
